package com.lmiot.xyclick.Bean.SQL;

import android.content.Context;
import com.lmiot.xyclick.Bean.SQL.DaoMaster;
import com.lmiot.xyclick.Bean.SQL.SearchBeanDao;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.greendao.query.WhereCondition;

public class SearchBeanSqlUtil {
    private static final SearchBeanSqlUtil ourInstance = new SearchBeanSqlUtil();
    private SearchBeanDao mSearchBeanDao;

    public static SearchBeanSqlUtil getInstance() {
        return ourInstance;
    }

    private SearchBeanSqlUtil() {
    }

    public void initDbHelp(Context context) {
        this.mSearchBeanDao = new DaoMaster(new DaoMaster.DevOpenHelper(context, "SearchBean-db", null).getWritableDatabase()).newSession().getSearchBeanDao();
    }

    public void add(SearchBean searchBean) {
        this.mSearchBeanDao.insertOrReplace(searchBean);
    }

    public void addList(List<SearchBean> list) {
        this.mSearchBeanDao.insertOrReplaceInTx(list);
    }

    public void update(SearchBean searchBean) {
        this.mSearchBeanDao.update(searchBean);
    }

    public void updateAll(List<SearchBean> list) {
        try {
            this.mSearchBeanDao.insertOrReplaceInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delByID(String str) {
        ArrayList arrayList = (ArrayList) this.mSearchBeanDao.queryBuilder().where(SearchBeanDao.Properties.KeyWord.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            this.mSearchBeanDao.delete((SearchBean) arrayList.get(0));
        }
    }

    public void delAll() {
        try {
            this.mSearchBeanDao.deleteInTx(this.mSearchBeanDao.queryBuilder().build().list());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<SearchBean> searchAll() {
        List<SearchBean> list = this.mSearchBeanDao.queryBuilder().orderAsc(SearchBeanDao.Properties.Id).build().list();
        return list == null ? new ArrayList() : list;
    }
}
